home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libd.lha / Lib / Des / MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  8.0 KB  |  354 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/des/RCS/misc.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1988 by the Massachusetts Institute of Technology.
  6.  *
  7.  * For copying and distribution information,
  8.  * please seethe file <mit-copyright.h>.
  9.  *
  10.  * This file contains most of the routines needed by the various
  11.  * make_foo programs, to account for bit- and byte-ordering on
  12.  * different machine types.  It also contains other routines useful in
  13.  * generating the intermediate source files.
  14.  */
  15.  
  16. #include <mit_copy.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include "des_intn.h"
  21.  
  22. /*
  23.  * The DES algorithm is defined in terms of MSBFIRST, so sometimes,
  24.  * e.g.  VAXes, we need to fix it up.  ANSI order means the DES
  25.  * MSBFIRST order.
  26.  */
  27.  
  28. #if 0 /* These don't seem to get used anywhere.... */
  29. void swap_bits(array)
  30.     char *array;
  31. {
  32. #ifdef MSBFIRST
  33.     /* just return */
  34.     return;
  35. #else /* LSBFIRST */
  36.     register old,new,i,j;
  37.  
  38.     /* for an eight byte block-- */
  39.     /* flips the bit order within each byte from 0 lsb to 0 msb */
  40.     for (i = 0; i<=7; i++) {
  41.         old = *array;
  42.         new = 0;
  43.         for (j = 0; j<=7; j++) {
  44.             new |= old & 01;    /* copy a bit */
  45.             if (j < 7) {
  46.                 /* rotate in opposite directions */
  47.                 old = old >> 1;
  48.                 new = new << 1;
  49.             }
  50.         }
  51.         *array++ = new;
  52.     }
  53. #endif /* MSBFIRST */
  54. }
  55.  
  56. unsigned long long_swap_bits(x)
  57.     unsigned long x;
  58. {
  59. #ifdef MSBFIRST
  60.     return x;
  61. #else
  62.     char *array = (char *) &x;
  63.     register old,new,i,j;
  64.  
  65.     /* flips the bit order within each byte from 0 lsb to 0 msb */
  66.     for (i = 0; i <= (sizeof(long)-1); i++) {
  67.         old = *array;
  68.         new = 0;
  69.         for (j = 0; j<=7; j++) {
  70.             if (old & 01)
  71.                 new = new | 01;
  72.             if (j < 7) {
  73.                 old = old >> 1;
  74.                 new = new << 1;
  75.             }
  76.         }
  77.         *array++ = new;
  78.     }
  79.     return x;
  80. #endif /* LSBFIRST */
  81. }
  82. #endif /* 0 */
  83.  
  84. unsigned long swap_six_bits_to_ansi(old)
  85.     unsigned long old;
  86. {
  87.     register unsigned long new, j;
  88.  
  89.     /* flips the bit order within each byte from 0 lsb to 0 msb */
  90.     new = 0;
  91.     for (j = 0; j<=5; j++) {
  92.         new |= old & 01;        /* copy a bit */
  93.         if (j < 5) {
  94.             /* rotate in opposite directions */
  95.             old = old >> 1;
  96.             new = new << 1;
  97.         }
  98.     }
  99.     return new;
  100. }
  101.  
  102. unsigned long swap_four_bits_to_ansi(old)
  103.     unsigned long old;
  104. {
  105.     register unsigned long new,j;
  106.  
  107.     /* flips the bit order within each byte from 0 lsb to 0 msb */
  108.     new = 0;
  109.     for (j = 0; j<=3; j++) {
  110.         new |= (old & 01);      /* copy a bit */
  111.         if (j < 3) {
  112.             old = old >> 1;
  113.             new = new << 1;
  114.         }
  115.     }
  116.     return new;
  117. }
  118.  
  119. unsigned long swap_bit_pos_1(x)
  120.     unsigned long x;
  121. {
  122.     /*
  123.      * This corrects for the bit ordering of the algorithm, e.g.
  124.      * bit 0 ==> msb, bit 7 lsb.
  125.      *
  126.      * given the number of a bit position, >=1, flips the bit order
  127.      * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
  128.      */
  129.     register y,z;
  130.  
  131.     /* always do it, only used by des_make_key_perm.c so far */
  132.     y = (x-1)/8;
  133.     z = (x-1)%8;
  134.  
  135.     x = (8-z) + (y*8);
  136.  
  137.     return x;
  138. }
  139.  
  140. unsigned long swap_bit_pos_0(x)
  141.     unsigned long x;
  142. {
  143.     /*  zero based version */
  144.  
  145.     /*
  146.      * This corrects for the bit ordering of the algorithm, e.g.
  147.      * bit 0 ==> msb, bit 7 lsb.
  148.      */
  149.  
  150. #ifdef MSBFIRST
  151.     return x;
  152. #else /* LSBFIRST */
  153.     register y,z;
  154.  
  155.     /*
  156.      * given the number of a bit position, >=0, flips the bit order
  157.      * each byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
  158.      */
  159.     y = x/8;
  160.     z = x%8;
  161.  
  162.     x = (7-z) + (y*8);
  163.  
  164.     return x;
  165. #endif /* LSBFIRST */
  166. }
  167.  
  168. unsigned long swap_bit_pos_0_to_ansi(x)
  169.     unsigned long x;
  170. {
  171.     /* zero based version */
  172.  
  173.     /*
  174.      * This corrects for the bit ordering of the algorithm, e.g.
  175.      * bit 0 ==> msb, bit 7 lsb.
  176.      */
  177.  
  178.     register unsigned long y,z;
  179.     /*
  180.      * given the number of a bit position, >=0, flips the bit order each
  181.      * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
  182.      */
  183.     y = x/8;
  184.     z = x%8;
  185.  
  186.     x = (7-z) + (y*8);
  187.  
  188.     return x;
  189. }
  190.  
  191. unsigned long rev_swap_bit_pos_0(x)
  192.     unsigned long x;
  193. {
  194.     /* zero based version */
  195.  
  196.     /*
  197.      * This corrects for the bit ordering of the algorithm, e.g.
  198.      *  bit 0 ==> msb, bit 7 lsb.
  199.      *
  200.      * Role of LSB and MSB flipped from the swap_bit_pos_0()
  201.      */
  202.  
  203. #ifdef LSBFIRST
  204.     return x;
  205. #else /* MSBFIRST */
  206.  
  207.     register y,z;
  208.  
  209.     /*
  210.      * given the number of a bit position, >=0, flips the bit order each
  211.      * byte. e.g. bit 3 --> bit 6, bit 13 --> bit 12
  212.      */
  213.     y = x/8;
  214.     z = x%8;
  215.  
  216.     x = (7-z) + (y*8);
  217.  
  218.     return x;
  219. #endif /* MSBFIRST */
  220. }
  221.  
  222. unsigned long swap_byte_bits(x)
  223.     unsigned long x;
  224. {
  225. #ifdef MSBFIRST
  226.     return x;
  227. #else /* LSBFIRST */
  228.  
  229.     char *array = (char *) &x;
  230.     register unsigned long old,new,j;
  231.  
  232.     /* flips the bit order within each byte from 0 lsb to 0 msb */
  233.     old = *array;
  234.     new = 0;
  235.     for (j = 0; j<=7; j++) {
  236.         new |= (old & 01);      /* copy a bit */
  237.         if (j < 7) {
  238.             old = old >> 1;
  239.             new = new << 1;
  240.         }
  241.     }
  242.     return new;
  243. #endif /* LSBFIRST */
  244. }
  245.  
  246. swap_long_bytes_bit_number(x)
  247.     unsigned long x;
  248. {
  249.     /*
  250.      * given a bit number (0-31) from a vax, swap the byte part of the
  251.      * bit number to change the byte ordering to mSBFIRST type
  252.      */
  253. #ifdef LSBFIRST 
  254.     return x;
  255. #else /* MSBFIRST */
  256.     unsigned long y,z;
  257.  
  258.     y = x/8;                    /* initial byte component */
  259.     z = x%8;                    /* bit within byte */
  260.  
  261.     x = (3-y)*8 +z;
  262.     return x;
  263. #endif /* MSBFIRST */
  264. }
  265.  
  266. void test_set(stream, src, testbit, dest, setbit)
  267.     FILE *stream;
  268.     const char *src;
  269.     int testbit;
  270.     const char *dest;
  271.     int setbit;
  272. {
  273. #ifdef DES_SHIFT_SHIFT
  274.     if (testbit == setbit)
  275.         fprintf(stream, "    %s |=  %s & (1<<%2d);\n",
  276.                 dest, src, testbit);
  277.     else
  278.         fprintf(stream, "    %s |= (%s & (1<<%2d)) %s %2d;\n",
  279.                 dest, src, testbit,
  280.                 (testbit < setbit) ? "<<" : ">>",
  281.                 abs(testbit - setbit));
  282. #else
  283.     fprintf(stream,
  284. #ifdef BITS16
  285.             "    if (%s & (1UL<<%2d))  %s |= 1UL<<%2d;\n",
  286. #else
  287.             "    if (%s & (1<<%2d))  %s |= 1<<%2d;\n",
  288. #endif /* BITS16 */
  289.             src, testbit, dest, setbit);            
  290. #endif
  291. }
  292.  
  293. extern void gen PROTOTYPE((FILE * stream));
  294. int des_debug;
  295. char const *whoami;
  296.  
  297. main(argc, argv)
  298.     int argc;
  299.     char *argv[];
  300. {
  301.     char *filename;
  302.     char *arg;
  303.     FILE * stream;
  304.  
  305.     whoami = argv[0];
  306.     filename = (char *)NULL;
  307.  
  308.     while (argc--, *++argv) {
  309.         arg = *argv;
  310.         if (*arg == '-') {
  311.             if (!strcmp(arg, "-d") && !strcmp(arg, "-debug"))
  312.                 des_debug++;
  313.             else {
  314.                 fprintf(stderr, "%s: unknown control argument %s\n",
  315.                         whoami, arg);
  316.                 goto usage;
  317.             }
  318.         }
  319.         else if (filename) {
  320.             fprintf(stderr,
  321.                     "%s: multiple file names provided: %s, %s\n",
  322.                     whoami, filename, arg);
  323.             goto usage;
  324.         }
  325.         else
  326.             filename = arg;
  327.     }
  328.  
  329.     if (!filename) {
  330.         fprintf(stderr, "%s: no file name provided\n", whoami);
  331.         goto usage;
  332.     }
  333.  
  334.     stream = fopen(filename, "w");
  335.     if (!stream) {
  336.         perror(filename);
  337.     usage:
  338.         fprintf(stderr, "usage: %s [-debug] filename\n", whoami);
  339.         exit(1);
  340.     }
  341.  
  342.     fputs(
  343.       "/* This file is automatically generated.  Do not edit it. */\n",
  344.           stream);
  345.  
  346.     /* This routine will generate the contents of the file. */
  347.     gen(stream);
  348.     if (fclose(stream) == EOF) {
  349.         perror(filename);
  350.         exit(1);
  351.     }
  352.     exit(0);
  353. }
  354.